﻿using EasyTest.Shared.TestAttribute;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ZLGCANoptimize
{
    public class USB
    {
        //static ZLGCANHelper zLGCANHelper = null;
        //static List<ZCAN_Receive_Data> zCAN_Receive_Datas = null;

        [ExposedMethodAttribute("")]
        public bool 切换USB音源(int 通道, int USB通道, out string 信息, out string data, out string 命令)
        {
            #if ELO20240927
            data = "";
            string cmd = "";
            命令 = "";
            if (USB通道 == 2)
            {
                cmd = "04 2E 4C 10 03 00 00 00";
            }
            else
            {
                cmd = "04 2E 4C 10 03 00 00 00";
            }
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                命令 = cmd;
                bool result1 = false;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);
                if (!result1)
                {
                    信息 = "发送数据失败,切换USB音源失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换USB音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换USB音源失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "切换USB音源失败" + ex.Message;
                return false;
            }
#else
            data = "";
            string cmd = "";
            命令 = "";
            //10 0C 2E 4C 20 01 00 00 
            //21 00 00 00 00 00 00 00
            if (USB通道 == 2)
            {
                cmd = "10 0C 2E 4C 10 03 00 00";
            }
            else
            {
                cmd = "10 0C 2E 4C 10 03 00 00";
            }
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                命令 = cmd;
                //10 0C 2E 4C 10 02 00 00
                // 21 00 00 00 00 00 00 00
                bool result1 = false, result2 = false;
                //增加循环次数，解决切换USB音源失败的问题
                for (int i = 0; i < 3; i++)
                {
                    result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                    Thread.Sleep(50);
                    result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 00 00 00 00 00 00", true, SendType.正常发送);
                    Thread.Sleep(100);
                }

                if (!result1 || !result2)
                {
                    信息 = "发送数据失败,切换USB音源失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换USB音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换USB音源失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "切换USB音源失败" + ex.Message;
                return false;
            }
#endif
        }

        [ExposedMethodAttribute("")]
        public bool 指定USB曲目(int 通道, int 曲目, out string 信息, out string data)
        {
#if ELO20240927
            data = "";
            string cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道,new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }

                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, "21 11 28 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置USB曲目失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 26"))
                        {
                            信息 = "设置USB曲目成功";
                            return true;
                        }
                    }
                }
                信息 = data;
            }
            catch (Exception ex)
            {
                信息 = "设置USB曲目失败" + ex.Message;
            }
            return false;
#else
            data = "";
            string cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00";
            //10 08 2E 4C 26 00 01 00
            //21 00 28 00 00 00 00 00
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }

                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, "21 00 28 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置USB曲目失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 26"))
                        {
                            信息 = "设置USB曲目成功";
                            return true;
                        }
                    }

                }
                //3、关闭
                信息 = data;
                return false;
            }
            catch (Exception ex)
            {
                信息 = "设置USB曲目失败" + ex.Message;
                return false;
            }
#endif
        }
        [ExposedMethodAttribute("")]
        public bool 播放指定USB曲目(int 通道, int 曲目, string 播放长度Hex, out string 信息, out string data)
        {
            data = "";
            string cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00";
            //10 08 2E 4C 26 00 01 00
            //21 00 28 00 00 00 00 00
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }

                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, $"21 00 {播放长度Hex} 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置USB曲目失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 26"))
                        {
                            信息 = "设置USB曲目成功";
                            return true;
                        }
                    }

                }
                //3、关闭
                信息 = data;
                return false;
            }
            catch (Exception ex)
            {
                信息 = "设置USB曲目失败" + ex.Message;
                return false;
            }

        }


        //=================================== 2024-09-27 ELO指令更新，以下方法为对应更新的内容 ==========================================
        [ExposedMethodAttribute("")]
        public bool 切换USB音源4C10(int 通道, int USB通道, out string 信息, out string data, out string 命令)
        {
            data = "";
            string cmd = "";
            命令 = "";
            if (USB通道 == 2)
            {
                cmd = "04 2E 4C 10 03 00 00 00";
            }
            else
            {
                cmd = "04 2E 4C 10 03 00 00 00";
            }
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                命令 = cmd;
                bool result1 = false;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);

                if (!result1)
                {
                    信息 = "发送数据失败,切换USB音源失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 10"))
                        {
                            信息 = "切换USB音源成功";
                            return true;
                        }
                    }
                }
                //3、关闭
                信息 = "切换USB音源失败";
                return false;
            }
            catch (Exception ex)
            {
                信息 = "切换USB音源失败" + ex.Message;
                return false;
            }
        }
        [ExposedMethodAttribute("")]
        public bool 指定USB曲目4C26(int 通道, int 曲目, out string 信息, out string data)
        {
            data = "";
            string cmd = $"10 08 2E 4C 26 00 {曲目.ToString("X2")} 00";
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }

                bool result1, result2;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);
                result2 = zLGCANHelper.SentData(通道, 0x761, "21 11 28 00 00 00 00 00", true, SendType.正常发送);
                if (!result1)
                {
                    信息 = "发送数据失败,设置USB曲目失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);// 03 6E 4C 10 AA AA AA AA
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        //10 0C 2E 4C 20 01 00 00
                        if (s.Trim().StartsWith("03 6E 4C 26"))
                        {
                            信息 = "设置USB曲目成功";
                            return true;
                        }
                    }
                }
                信息 = data;
            }
            catch (Exception ex)
            {
                信息 = "设置USB曲目失败" + ex.Message;
            }
            return false;
        }
        [ExposedMethodAttribute("")]
        public bool 停止播放USB音源4C20(int 通道, int USB通道, out string 信息, out string data, out string 命令)
        {
            data = "";
            string cmd = "";
            命令 = "";
            if (USB通道 == 2)
            {
                cmd = "04 2E 4C 20 02 00 00 00";
            }
            else
            {
                cmd = "04 2E 4C 20 02 00 00 00";
            }
            try
            {
                if (!数据.初始化(通道, out ZLGCANHelper zLGCANHelper))
                {
                    信息 = "启动can设备失败";
                    return false;
                }
                bool OK = Can.解密(通道, new List<ZCAN_Receive_Data>(), out 信息);
                if (!OK)
                {
                    信息 = "解密失败";
                    return false;
                }
                命令 = cmd;
                bool result1 = false;
                result1 = zLGCANHelper.SentData(通道, 0x761, cmd, true, SendType.正常发送);
                Thread.Sleep(50);

                if (!result1)
                {
                    信息 = "发送数据失败,停止播放USB音源失败";
                    return false;
                }
                List<ZCAN_Receive_Data> zCAN_Receive_Datas = zLGCANHelper.RecvData(通道, 0x769, 3, 1500);
                string[] datas = ZLGCanTool.GetData(zCAN_Receive_Datas);
                data = string.Join("\n", datas);
                if (datas.Length > 0)
                {
                    foreach (string s in datas)
                    {
                        if (s.Trim().StartsWith("03 6E 4C 20"))
                        {
                            信息 = "停止播放USB音源成功";
                            return true;
                        }
                    }
                }
                信息 = "停止播放USB音源失败";
            }
            catch (Exception ex)
            {
                信息 = "停止播放USB音源失败" + ex.Message;
            }
            return false;
        }
    }
}
